package cn.weixindong.com;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.collections.set.SynchronizedSet;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.weixindong.com.dao.mapper.BranchMapper;
import cn.weixindong.com.pojo.Branch;
import cn.weixindong.com.pojo.User;

/**
 * Handles requests for the application home page.
 */

@Controller
public class HomeController {
	
	
	@Autowired
	private BranchMapper mapper;
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	
	/**
	 * 进入登陆页面
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET,
			produces="application/json;charset=utf-8")
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Branch selectByPrimaryKey = mapper.selectByPrimaryKey(11);
		System.out.println(selectByPrimaryKey.toString());
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
	
	/**
	 * 进入登陆
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/subLogin", method = RequestMethod.POST)
	@ResponseBody
	public String sublogin(User user) {
		
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getUsername(), user.getPassword());
		
		try {
			subject.login(usernamePasswordToken);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.getMessage();
		}
		System.out.println("权限1"+subject.hasRole("admin"));
		System.out.println("权限2"+subject.hasRole("admin1"));
		System.out.println("权限3"+subject.hasRole("user"));
		System.out.println(subject.isAuthenticated());
		return "dengluchengg";
	}
	
	/**
	 * 退出登陆
	 * @return
	 */
	@RequestMapping(value = "/loginout")
	@ResponseBody
	public String loginout() {
		
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "推出成功";
		
		
	}
	
	
	@RequiresRoles("admin")
	@ResponseBody
	@RequestMapping("/testRole")
	public String testRole() {
		
		return "testRole success";
	}
	
	
	@RequiresRoles("admin1")
	@ResponseBody
	@RequestMapping("/testRole1")
	public String testRole1() {
		
		return "testRole1 success";
	}
	
	
	@RequiresPermissions("user:delete")
	@ResponseBody
	@RequestMapping("/testRole3")
	public String testRole3() {
		
		return "testRole3 success";
	}
	
	
	@RequestMapping("/testRole4")
	public String testRole4() {
		
		return "pp";
	}
	
	
	@RequestMapping("/testRole5")
	public String testRole5() {
		
		return "pp";
	}
	
	
}
